<script type="text/javascript">
    RED.nodes.registerType('ui_form',{
        category: RED._("node-red-dashboard/ui_base:ui_base.label.category"),
        color: 'rgb(176, 223, 227)',
        defaults: {
            name: {value: ''},
            label: {value: ''},
            group: {type: 'ui_group', required: true},
            order: {value: 0},
            width: {value: 0, validate: function(v) {
                    var width = v||0;
                    var currentGroup = $('#node-input-group').val()||this.group;
                    var groupNode = RED.nodes.node(currentGroup);
                    var valid = !groupNode || +width <= +groupNode.width;
                    $("#node-input-size").toggleClass("input-error",!valid);
                    return valid;
                }
            },
            height: {value: 0},
            options: {value:[{value:'', label :'', type:'', required:true}], validate:function(value) {
                    if (value.length ) {
                        for (var i = 0; i < value.length; i++) {
                            if (!value[i].value) {
                                return false;
                            }
                        }
                    }
                    else {
                        return false;
                    }
                    return true;
                }, required:true},
            formValue:{value:{}},
            payload: {value: ''},
            submit: {value: "submit"},
            cancel: {value: "cancel"},
            topic: {value: 'topic', validate: (RED.validators.hasOwnProperty('typedInput')?RED.validators.typedInput('topicType'):function(v) { return true})},
            topicType: {value: 'msg'},
            splitLayout: {value:''},
            className: {value: ''}
        },
        inputs:1,
        outputs:1,
        icon: "ui_form.png",
        paletteLabel: 'form',
        label: function() { return this.name || this.label || 'form'; },
        labelStyle: function() { return this.name?"node_label_italic":""; },
        oneditprepare: function() {
            if ($("#node-input-submit").val() === null) { $("#node-input-submit").val("submit"); }
            if ($("#node-input-cancel").val() === null) { $("#node-input-cancel").val("cancel"); }
            $("#node-input-size").elementSizer({
                width: "#node-input-width",
                height: "#node-input-height",
                group: "#node-input-group"
            });

            this.resizeRule = function(option,newWidth) {
                //option.find(".node-input-option-type").width(newWidth);
                //  option.find(".node-input-option-label").width(newWidth);
                //  option.find(".node-input-option-value").width(newWidth);
            }

            function generateOption(i, option) {
                var container = $('<li/>',{style:"margin:0; padding:8px 0px 0px; border-bottom:1px solid #ccc;"});
                var row = $('<div/>').appendTo(container);
                var row2 = $('<div/>',{style:"padding-top:5px; padding-left:175px;"}).appendTo(container);
                var row3 = $('<div/>',{style:"padding-top:5px; padding-left:120px;"}).appendTo(container);

                $('<i style="cursor:move; margin-left:3px;" class="node-input-option-handle fa fa-bars"></i>').appendTo(row);

                var labelField = $('<input/>',{class:"node-input-option-label", type:"text", style:"margin-left:7px; width:20%;", placeholder: RED._("node-red-dashboard/ui_form:ui_form.label.egName"), value:option.label}).appendTo(row);//.typedInput({default:'str',types:['str', 'num']});
                var valueClass ="node-input-option-value"
                if (!option.value) { valueClass ="node-input-option-value input-error"; }
                var valueField = $('<input/>',{class:valueClass, type:"text", style:"margin-left:7px; width:20%;", placeholder: RED._("node-red-dashboard/ui_form:ui_form.label.egName2"), value:option.value}).appendTo(row);//.typedInput({default:'str',types:['str','num','bool']});
                valueField.keyup(function() {
                    if ($(this).val() && $(this).hasClass('input-error')) {
                        $(this).removeClass('input-error')
                    }
                    else {
                        if (!$(this).val()) {
                            $(this).addClass('input-error')
                        }
                    }

                });
                // var typeField = $('<input/>',{class:"node-input-option-type",type:"text",style:"margin-left: 7px; width: 135px;", placeholder: 'Type', value:option.type}).appendTo(row).typedInput({default:'str',types:['str', 'num']});
                var typeField = $('<select/>',{class:"node-input-option-type",type:"text",style:"margin-left:7px; width:16%"}).appendTo(row);//.typedInput({default:'str',types:['str', 'num']});

                var arr = [
                  {val : "text", text: RED._("node-red-dashboard/ui_form:ui_form.label.text")},
                  {val : "multiline", text: RED._("node-red-dashboard/ui_form:ui_form.label.multiline")},
                  {val : "number", text: RED._("node-red-dashboard/ui_form:ui_form.label.number")},
                  {val : "email", text: RED._("node-red-dashboard/ui_form:ui_form.label.email")},
                  {val : "password", text: RED._("node-red-dashboard/ui_form:ui_form.label.password")},
                  {val : "checkbox", text: RED._("node-red-dashboard/ui_form:ui_form.label.checkbox")},
                  {val : "switch", text: RED._("node-red-dashboard/ui_form:ui_form.label.switch")},
                  {val : "date", text: RED._("node-red-dashboard/ui_form:ui_form.label.date")},
                  {val : "time", text: RED._("node-red-dashboard/ui_form:ui_form.label.time")}
                ];

                //var sel = $('<select>').appendTo('body');
                $(arr).each(function() {
                    var isSelected= false;
                    if (option.type == this.val) {
                        isSelected = true;
                    }
                    typeField.append($("<option>").attr('value',this.val).text(this.text).prop('selected',isSelected));
                });

                //var labelForRequried = $('<span/>',{style:"margin: 10px;"}).text('Required').appendTo(row);
                var requiredContainer= $('<div/>',{style:"display:inline-block; height:34px; width:13%; vertical-align: middle"}).appendTo(row);
                var requiredInnerContainer= $('<div/>',{style:"left:35%; position:relative; width:30px"}).appendTo(requiredContainer);
                var reqRow=$("<label />",{class:"switch",style:"top:10px; width:30px;"}).appendTo(requiredInnerContainer);
                //var required = $('<input/>',{class:"node-input-option-required",style:"margin: 5px;width:19%",type:"checkbox", checked:option.required}).appendTo(row);//labelForRequried);//.typedInput({default:'str',types:['str', 'num']});
                var required = $('<input/>',{class:"node-input-option-required", type:"checkbox", checked:option.required, style:"vertical-align:top;"}).appendTo(reqRow);//labelForRequried);//.typedInput({default:'str',types:['str', 'num']});
                var reqDiv=$("<div />",{class:"slider round"}).appendTo(reqRow);
                var vis = option.rows ? 'visible' : 'hidden';
                var rowsField = $('<input/>',{class:"node-input-option-rows", type:"number", style:"width:10%;", placeholder:'Rows', value:option.rows }).css('visibility',vis).appendTo(row);

                var finalspan = $('<div/>',{style:"display:inline-block; width:5%;"}).appendTo(row);
                var deleteButton = $('<a/>',{href:"#",class:"editor-button", style:"font-size:1.3em; left:45%; position:relative;"}).appendTo(finalspan);
                $('<i/>',{class:"fa fa-trash-o"}).appendTo(deleteButton);

                typeField.change(function(e){
                    if (e.target.value != 'multiline') {
                        rowsField.val(undefined)
                        option.rows = null;
                        rowsField.css('visibility','hidden')
                    } else {
                        rowsField.css('visibility','visible')
                        if (!rowsField[0].value) rowsField[0].value = 3;
                    }

                })

                deleteButton.click(function() {
                    container.find(".node-input-option-value").removeAttr('required')
                    container.css({"background":"#fee"});
                    container.fadeOut(300, function() {
                        $(this).remove();
                    });
                });

                $("#node-input-option-container").append(container);
            }

            $("#node-input-add-option").click(function() {
                generateOption($("#node-input-option-container").children().length+1, {});
                $("#node-input-option-container-div").scrollTop($("#node-input-option-container-div").get(0).scrollHeight);
            });

            for (var i=0; i<this.options.length; i++) {
                var option = this.options[i];
                generateOption(i+1,option);
            }

            $('#node-input-topic').typedInput({
                default: 'str',
                typeField: $("#node-input-topicType"),
                types: ['str','msg','flow','global']
            });

            $( "#node-input-option-container" ).sortable({
                axis: "y",
                handle:".node-input-option-handle",
                cursor: "move"
            });
        },
        oneditsave: function() {
            var options = $("#node-input-option-container").children();
            var node = this;
            node.options = [];
            node.formValue = {};
            options.each(function(i) {
                var option = $(this);
                var o = {
                    label: option.find(".node-input-option-label").val(),//typedInput('value'),
                    value: option.find(".node-input-option-value").val(),//typedInput('value'),
                    type: option.find(".node-input-option-type").val(),//typedInput('value')
                    required: option.find(".node-input-option-required").is(':checked'),
                    rows: parseInt(option.find(".node-input-option-rows").val())
                };
                // o.value= o.value||o.label||(o.type+"_"+i);
                node.formValue[o.value]= o.type == "checkbox" || o.type == "switch" ? false : "";
                node.options.push(o);
            });
        },
        oneditresize: function() {
            var options = $("#node-input-option-container").children();
            var newWidth = ($("#node-input-option-container").width() - 175)/2;
            var node = this;
            options.each(function(i) {
                node.resizeRule($(this),newWidth);
            });
        }
    });
</script>
<script type="text/html" data-template-name="ui_form">

<style>
.switch {
    position: relative;
    display: inline-block;
    width: 30px;
    height: 18px;
}

.switch input {display:none;}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    -webkit-transition: .4s;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 15px;
    width: 15px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    -webkit-transition: .4s;
    transition: .4s;
}

input:checked + .slider {
    background-color: #910000;
}

input:focus + .slider {
    box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
    -webkit-transform: translateX(11px);
    -ms-transform: translateX(11px);
    transform: translateX(11px);
}

/* Rounded sliders */
.slider.round {
    border-radius: 34px;
}

.slider.round:before {
    border-radius: 50%;
}
</style>

    <div class="form-row">
        <label for="node-input-group"><i class="fa fa-table"></i> <span data-i18n="ui_form.label.group"></label>
        <input type="text" id="node-input-group">
    </div>
    <div class="form-row">
        <label><i class="fa fa-object-group"></i> <span data-i18n="ui_form.label.size"></label>
        <input type="hidden" id="node-input-width">
        <input type="hidden" id="node-input-height">
        <button class="editor-button" id="node-input-size"></button>
    </div>
    <div class="form-row">
        <label for="node-input-label"><i class="fa fa-tag"></i> <span data-i18n="ui_form.label.label"></label>
        <input type="text" id="node-input-label" data-i18n="[placeholder]ui_form.label.optionalLabel">
    </div>
    <div class="form-row node-input-option-container-row" style="margin-bottom:0px; width:100%; min-width:520px">
        <label style="vertical-align:top;"><i class="fa fa-list-alt"></i> <span data-i18n="ui_form.label.formElements"></label>
        <div style="display:inline-block; width:78%; border:1px solid #ccc; border-radius:5px; box-sizing:border-box;">
          <div class="red-ui-tray-header" style="width:100%; display: inline-block; padding-top:10px; padding-buttom:10px; border-top:0px solid; border-radius:5px 5px 0 0; border-bottom:1px solid #ccc;">
              <div style="width:94%; display:inline-block; margin-left:27px">
                <div style="width:20%; text-align:center; float:left;" data-i18n="ui_form.label.label"></span></div>
                <div style="width:20%; text-align:center; float:left; margin-left:9px" data-i18n="node-red:common.label.name"></div>
                <div style="margin-left:7px; width:16%; text-align:center; float:left; margin-left:9px" data-i18n="ui_form.label.type"></div>
                <div style="width:16%; text-align:center; float:left;" data-i18n="ui_form.label.required"></div>
                <div style="width:10%; text-align:center; float:left;" data-i18n="ui_form.label.rows"></div>
                <div style="width:12%; text-align:center; float:left;" data-i18n="ui_form.label.remove"></div>
              </div>
          </div>
          <div id="node-input-option-container-div" style=" height: 257px; padding: 5px; overflow-y:scroll;">
            <ol id="node-input-option-container" style=" list-style-type:none; margin: 0;"></ol>
          </div>
        </div>
    </div>
    <div class="form-row">
        <a href="#" class="editor-button editor-button-small" id="node-input-add-option" style="margin-top: 4px; margin-left: 103px;"><i class="fa fa-plus"></i> <span data-i18n="ui_form.label.element"></span></a>
    </div>
    <div class="form-row">
        <label for="node-input-submit"><i class="fa fa-square"></i> <span data-i18n="ui_form.label.buttons"></label>
        <i class="fa fa-thumbs-o-up"></i> <input type="text" id="node-input-submit" data-i18n="[placeholder]ui_form.label.submitButtonText" style="width:35%;">
        <span style="margin-left:16px"><i class="fa fa-thumbs-o-down"></i></span>
        <input type="text" id="node-input-cancel" data-i18n="[placeholder]ui_form.label.cancelButtonText" style="width:35%;">
    </div>
    <div class="form-row">
        <label for="node-input-splitLayout"></label>
        <input type="checkbox" id="node-input-splitLayout" style="display:inline-block; width:auto; vertical-align:top;">
        <span data-i18n="ui_form.label.splitLayout">
    </div>
    <div class="form-row">
        <label for="node-input-topic"><i class="fa fa-tasks"></i> <span data-i18n="ui_form.label.topic"></label>
        <input type="text" id="node-input-topic" data-i18n="[placeholder]ui_form.label.optionalMsgTopic">
        <input type="hidden" id="node-input-topicType">
    </div>
    <div class="form-row">
        <label for="node-input-className"><i class="fa fa-code"></i>  <span data-i18n="ui_form.label.className"></label>
        <input type="text" id="node-input-className" data-i18n="[placeholder]ui_form.label.classNamePlaceholder"/>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></label>
        <input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
    </div>
</script>
